import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:myturngame/colors.dart';

import '../../Theme.dart';
import '../../bean/combat/damage.dart';

class DamageText extends StatefulWidget {
  Damage damage;

  DamageText(this.damage, {Key? key}) : super(key: key);

  @override
  State<DamageText> createState() => _DamageTextState();
}

class _DamageTextState extends State<DamageText>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;

  var targetOffset = const Offset(0, -2);

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
        duration: const Duration(milliseconds: 1000), vsync: this)
      ..reset()
      ..forward();
  }

  @override
  Widget build(BuildContext context) {
    var offsetAnimation = Tween(begin: const Offset(0, -1), end: targetOffset)
        .animate(_controller);
    var text = "-${widget.damage.value}";
    if (widget.damage.addUpValue > 0) {
      text = "$text (${widget.damage.addUpValue})";
    }
    var textColor = damageColor;
    var fontSize = 20.0;
    if (widget.damage.isCritical) {
      text = "暴$text";
      textColor = damageCriticalColor;
    } else if (widget.damage.isSkillDamage) {
      text = "猛$text";
      textColor = damageCriticalColor;
    }
    if (text.length > 4) {
      fontSize = 20 - (text.length - 4) * 3;
    }

    return SlideTransition(
        position: offsetAnimation,
        child: Text(text,
            style: TextStyle(
                color: textColor,
                fontSize: fontSize,
                decoration: TextDecoration.none,
                shadows: [
                  Shadow(color: Colors.black.withOpacity(0.5),blurRadius: 3)
                ])));
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }
}

class DamageTextPainter extends CustomPainter{
  List<Damage> damageList;

  DamageTextPainter(this.damageList);

  @override
  void paint(Canvas canvas, Size size) {
    Paint textPaint = Paint()
      ..color = Colors.black
      ..strokeWidth = 2.0
      ..style = PaintingStyle.fill
      ..strokeCap = StrokeCap.round;

    canvas.drawRect(Rect.largest, textPaint);

    TextSpan span = TextSpan(
      text: 'Hello, Flutter!',
      style: TextStyle(
        fontSize: 20.0,
        color: damageColor
      ),
    );
    TextPainter textPainter = TextPainter(
      text:span,
    );
    textPainter.layout();
    textPainter.paint(canvas, const Offset(0, 0));
    canvas.restore();
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return false;
  }
}
